define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('@mod/fieldEdit').base;

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				urlRemove: 'ptc/remove'
				,urlRemoveAll: 'ptc/removeAll'
			});
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			this.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('图文创意渠道表')+'</span>',
					'<div class="mb10">',
						'<span class="fl pr5" pub-name="importFile" pub-mod="@base/common/input.button"/>',
						'<span class="" pub-name="generate" pub-mod="@base/common/input.button"/>',
					'</div>',
					'<div class="mb10">',
						'<span class="pr5" pub-name="add" pub-mod="@base/common/input.button"/>',
						'<span class="" pub-name="removeAll" pub-mod="@base/common/input.button"/>',
					'</div>',
				'</div>',
				'<div class="" pub-name="grid" pub-mod="ptc/grid/adx.base"/>'
			].join('')));


			this.buildFromTemplate({
				importFile: {
					value: '上传文件进行导入'
					,'class': 'btn btn-primary'
					,width: null
				},
				generate: {
					value: '导出JSON格式'
					,'class': 'btn btn-primary'
					,width: null
				},
				add: {
					value: '单个分类添加'
					,'class': 'btn btn-success'
					,width: null
				},
				removeAll: {
					value: '清除所有数据'
					,'class': 'btn btn-danger'
					,width: null
				},
				grid: {
					'hasSearch': true,
					'hasFilter': false
				}
			});

			self.createAsync('editPop','ptc/popwin/adx.base');

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
				]
			});

		}
		,onInputClick: function(ev) {
			var c = this.getConfig();
			var url = '';
			switch (ev.name) {
				case 'removeAll':
					return pubjs.confirm(LANG('真的要删除所有图文创意的配置吗?'), function(state) {
						if(state)
						{
							pubjs.loading.show();
							pubjs.mc.send(c.urlRemoveAll, function(err, data) {
								pubjs.loading.hide();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.alert(LANG('删除成功记录数%1', data));
							})
						}

					});
					break;
				case 'generate':
					return window.open('api/admin/ptcGenerate');
					break;
				case 'importFile':
					url = 'ptc/import';
					break;
				case 'add':
					var mod = this.$.editPop;
					mod && mod.reset().show();
					return;
				default :
					return false;
			}

			pubjs.controller.navigate(url);
		}
		,reload: function() {
			this.$.grid.load();
		}
		,reset: function() {

		}
		,onSaveRecord: function() {
			var self = this;
			var mod = self.$.editPop;
			mod && mod.hide();
			self.$.grid.reload();
		}
		,onListOpClick:function(ev){
			//编辑单条记录
			var self = this;
			var c = self.getConfig();
			var id = ev.param.data._id;
			if(ev.param.op == 'edit') {
				var mod = self.$.editPop;
				mod && mod.reset().reload(id).show();
			}
			else if(ev.param.op == 'del') {
				pubjs.confirm(LANG('确认要删除此记录吗, 此删除是不可恢复的, 如有子类也会随之删除'), function(res) {
					if(res) {
						pubjs.loading.show();
						pubjs.mc.send(c.urlRemove, {_id: id}, function(err) {
							pubjs.loading.hide();
							if(err) {
								pubjs.alert(err.message);
								return false;
							}

							pubjs.alert(LANG('删除成功'));
							self.reload();
						});
					}
				});
			}
			return false;
		}
	});

	exports.list = List;

	exports.edit = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{text: LANG('Name'), tip: LANG('模板的名称'), type: 'input', field: 'name', validate: true}
					,{text: LANG('AdxId'), tip: LANG('渠道Id'), type: 'input', field: 'AdxId', validate: true}
				],
				Title: LANG('渠道信息:'),
				urlUpdate: 'ptc/update',
				urlAdd: 'ptc/add',
				urlInfo: 'ptc/info'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,dataChange: function(field, v) {
			this.Super('dataChange', arguments);
			// do whatever you want
		}
		,setData: function() {
			this.Super('setData', arguments);
			this.disable('AdxId');
			this.disable('AdxType');
			return this;
		}
	});
});